package scapi.utilerias;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class BD {
	private String host;
	private String user;
	private String pass;
	private String database;
	private Connection conn;

	public BD() throws SQLException{
		this.host="localhost";
		this.user="root";
		this.pass="pass";
		this.database="scapi";

		open();
	}

	public BD(String user, String pass, String database) throws SQLException {
		this.user = user;
		this.pass = pass;
		this.database = database;
		open();
	}

	private void open() throws SQLException{
		String urljdbc = "jdbc:mysql://" + this.host + ":" + 3306 + "/" + this.database;
		this.conn = DriverManager.getConnection(urljdbc,this.user,this.pass);
	}

	public void close() throws SQLException{
		this.conn.close();
	}

	public ResultSet execute(String consulta) throws SQLException{
		Statement stm = this.conn.createStatement();
		return stm.executeQuery(consulta);
	}

	public ResultSet executeRecord(String nomTab, String filtro) throws SQLException{
		Statement stm = this.conn.createStatement();
		return stm.executeQuery("SELECT * FROM "+nomTab+" WHERE " + filtro);
	}

	public ResultSet executeField(String campo, String nomTab, String filtro) throws SQLException{
		Statement stm = this.conn.createStatement();

		System.out.println("SELECT "+campo+" FROM "+nomTab+" WHERE " + filtro);
		return stm.executeQuery("SELECT "+campo+" FROM "+nomTab+" WHERE " + filtro);

	}

	public ResultSet executeField(String campo, String nomTab) throws SQLException{
		Statement stm = this.conn.createStatement();

		return stm.executeQuery("SELECT "+campo+" FROM "+nomTab);
	}

	public ResultSet executeTable (String nomTab, String orden) throws SQLException{
		Statement stm = this.conn.createStatement();
		return stm.executeQuery("SELECT * FROM " + nomTab+ " ORDER BY "+ orden);
	}

	public ResultSet executeTable (String nomTab) throws SQLException{
		Statement stm = this.conn.createStatement();
		return stm.executeQuery("SELECT * FROM " + nomTab);
	}

	public void executeScalar(String sentencia) throws SQLException{
		Statement stm = this.conn.createStatement();
		stm.executeUpdate(sentencia);

	}

	public int recordCount(String nombreTabla) throws SQLException{

		ResultSet rs = this.execute("SELECT COUNT(*) FROM " + nombreTabla);
		rs.next();
		return rs.getInt(1);
	}

	public void executeInsert(String tabla, String valores) throws SQLException{
		Statement stm = this.conn.createStatement();
		String consulta = "INSERT INTO "+tabla+" VALUES ("+valores+");";
		System.out.print(consulta);
		stm.executeUpdate(consulta);
	}

	public void delete(String tabla,String condicion) throws SQLException{
		Statement stm = this.conn.createStatement();
		String consulta = "DELETE FROM "+tabla+" WHERE "+condicion;
		stm.executeUpdate(consulta);
	}
	public void update(String tabla,String valores,String condicion) throws SQLException{
		Statement stm = this.conn.createStatement();
		String updte = "UPDATE "+tabla+" SET "+valores+" WHERE "+condicion;
		System.out.println(updte);
		stm.executeUpdate(updte);
	}

	public int getLasID() throws SQLException{
		Statement stm = this.conn.createStatement();
		ResultSet rs = stm.executeQuery("SELECT LAST_INSERT_ID()");
		rs.next();
		return rs.getInt(1);

	}
	public int getLasID(String tabla) throws SQLException{
		Statement stm = this.conn.createStatement();
		ResultSet rs = stm.executeQuery("SELECT LAST_INSERT_ID() FROM " + tabla);
		rs.next();
		return rs.getInt(1);

	}

	public ResultSet ultimoDato(String columna,String tabla) throws SQLException{
		Statement stm = this.conn.createStatement();
		ResultSet rs = stm.executeQuery("SELECT MAX("+columna+") AS "+columna+" FROM "+tabla+"");
	    return rs;
	}

	public void reset() throws SQLException{
		this.conn.setAutoCommit(true);

	}

        public Connection getConn(){
            return this.conn;
        }




	}
